//-----------------------------------------C_estout_esttab_app_fe_non-imp.do
// Appendix C fe and non-imputed
// --------------------------------------------------------------------------
version 15 // ensure compatibility
clear all // start in a clean state
cls // clear the results window
set more off // show all output, dont wait if screen full
set seed 12345 // see note below
set rmsg on // displays how time to execute command s
cd "C:\Users\wachb\OneDrive - UvA\WInequality\Replicate_Analysis"
capture log close // closes any log file if currently open
log using C_estout_esttab_app_fe_non-imp, replace text

//open the data
use "C:\Users\wachb\OneDrive - UvA\WInequality\Replicate_Analysis\countryxyzcorr.dta"

//The estout package provides tools for making regression tables. 
//eststo stores a copy of the active estimation results for later tabulation. (It is an alternative to official Stata's estimates store.)
//esttab is a command to make publication-style tables.
//fe stands for panel fixed effects, real stands for non-imputed wealth data

label var meanUDS "UDS"
label var RGTQ "Social Trust"
label var MeanGiniSWIID "Gini Income"
label var MeanGDP "GDP"
label var GiniWealth "Gini Wealth"
label var prepr "Trust in Representative Institutions"
label var pimpl "Trust in Implementing Institutions"
label var z_meanUDS "UDS"
label var z_logRGTQ "Social Trust"
label var z_MeanGiniSWIID "Gini Income"
label var z_logMeanGDP "GDP"
label var z_GiniWealth "Gini Wealth"
label var z_prepr "Trust in Representative Institutions"
label var z_pimpl "Trust in Implementing Institutions"
label var t "Time Interval"
label var z_prepr_wg "Trust in Representative Institutions (excl. item on government)"

order ISO3166_2-logRGTQ GTQmis preprimplmis prepr_wg GTQmis preprimplmis ///
z_GiniWealth-z_meanUDS z_logMeanGDP z_logRGTQ z_prepr z_pimpl z_prepr_wg

//realwealthdata ==> if real == 1 | old == 1
//Appendix Tables C
//eststo and esttab
foreach var of varlist(z_logRGTQ z_prepr z_pimpl){
	eststo clear
	local time i.t
	local robust cluster(ISO3166_1_num1)
	local controls z_logMeanGDP z_meanUDS
	xtset ISO3166_1_num1 t
	/*model PFE 1*/eststo: quie xtreg `var' z_GiniWealth z_MeanGiniSWIID `time', fe vce(robust)
	/*model PFE 2*/eststo: quie xtreg `var' z_GiniWealth z_MeanGiniSWIID `controls' `time', fe vce(robust)
	/*model Non-Imp.1*/eststo: quie reg `var' z_GiniWealth z_MeanGiniSWIID `time' if real == 1 | old == 1, `robust'
	/*model Non-Imp.2*/eststo: quie reg `var' z_GiniWealth z_MeanGiniSWIID `controls' `time' if real == 1 | old == 1, `robust'
	local name = subinstr("`var'", "z_", "",.)
	local name2 = subinstr("`name'", "logR", "",.)
	local name3 = subinstr("`name2'", "GTQ", "1_GTQ",.) 
	local name4 = subinstr("`name3'", "prepr", "2_prepr",.)
	local name5 = subinstr("`name4'", "pimpl", "3_pimpl",.)
	esttab using C`name5'_fe_non-imp.rtf, order(z_GiniWealth z_MeanGiniSWIID z_logMeanGDP z_meanUDS) ///
	b(3) se(3) r2 varwidth(17) compress label nobaselevels nogaps ///
	noomitted interaction(" \f1 x \f0") title(Panel Fixed effects and Non-Imputed Wealth Data models of `"`: var label `var''"') ///
	nonumbers mtitles("PFE Model 1 Coefficient (SE)" "PFE Model 2 Coefficient (SE)" "Non-Imp. Model 1 Coefficient (SE)" "Non-Imp. Model 2 Coefficient (SE)") ///
	nonotes addnotes("Note.---SE = Robust standard errors clustered at country level; GDP = Gross Domestic Product per capita at purchasing power parity 2017 international $; UDS = Unified Democracy Score; GDP and Social Trust transformed using natural logarithm, all variables z-standardized." "* p < 0.05." "** p < 0.01." "*** p < 0.001.") ///
	fonttbl(\f0\fnil Times New Roman;\f1\fnil Arial;) substitute("\fs20" "\fs24" "Interval=" "Interval " "Observations" "\i N" " p " "{\i p }" "*" "{\super *}") replace
}

//manual changes: put Panel Fixed Effect, Non-Imputed Wealth Data, Model #, and Coefficient (SE) in seperate rows, added a line between first and second rows, ///
///removed line above N, removed social trust in the note in tables on political trust, added # in document name     